const app = getApp()
var netUtil = require("../../utils/netUtil.js"); //require引入
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的分享', //导航栏 中间的标题
    },
    height: app.globalData.height * 2 + 20,
    openSettingBtnHidden: true,
    saveImgBtnHidden: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.setData({
      type: options.type
      
    })
    this.imageLoad()
    if (options.type =='hyzl'){//好友助力
      wx.downloadFile({
        url: app.globalData.url + '/help-qrcode?order_pay_id=' + options.order_pay_id,
        success: function (res3) {
          //背景图
          console.log('二维码图片缓存本地成功')
          that.setData({
            erCodeImg: res3.tempFilePath
          })

          that.createGoodShareImg();
        }
      })
      this.setData({
        order_pay_id:options.order_pay_id
      })

    } else {//我的分享
      wx.downloadFile({
        url: app.globalData.url + "/user/inviteQrcode?token=" + wx.getStorageSync("token"),//网络路径  goods_img: options.goods_img,
        success: function (res3) {
          //背景图
          console.log('二维码图片缓存本地成功')
          that.setData({
            erCodeImg: res3.tempFilePath
          })
          that.createGoodShareImg();
         
        }
      })
      
    }
    this.setData({
      token: wx.getStorageSync("userInfo").token,
      type: 'dl'
    })
    var that=this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    });

   

    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  imageLoad: function (e) {
    var that=this
    var src='';
    if (that.data.type == 'dl'){
      src = '/image/posterDl3.jpg'
    }else{
      src ='/image/poster2.png'
    }
    wx.getImageInfo({
      src: src,
      success: function (res) {
        console.log(res.height);
        console.log(res.width)
        that.setData({
          imgheight: res.height/2,
          imgWidth: res.width/2
          
        })
      }
    })
   
  },
  createGoodShareImg() {
    wx.showLoading({
      title: '加载中',
    })
    
    var that = this;
   
    var windowWidth = this.data.windowWidth;
    var windowHeight = this.data.windowHeight;
   
    const ctx = wx.createCanvasContext('myCanvas')
    //背景图
    if (that.data.type=='dl'){
      ctx.drawImage("/image/poster2.png", 0, 0, that.data.imgWidth, that.data.imgheight)
      ctx.fill();


      //二维码
      ctx.drawImage(that.data.erCodeImg,25, 559, 95,95)
      ctx.draw()
      setTimeout(() => {
        // 将生成的canvas图片，转为真实图片
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          canvasId: 'myCanvas',
          success: function (res) {
            let shareImg = res.tempFilePath;
            that.setData({
              shareImg: shareImg,
            })
            wx.hideLoading();
          },
          fail: function (res) { }
        })
      }, 500)
    }else{
      ctx.drawImage("/image/poster2.png", 0, 0, that.data.imgWidth, that.data.imgheight)
      ctx.fill();

      ctx.beginPath() //开始创建一个路径

      ctx.arc(63,520, 46, 0, 2 * Math.PI, false) //画一个圆形裁剪区域
      ctx.setStrokeStyle('rgba(0,0,0,0)');
      ctx.stroke();

      ctx.clip(); //裁剪
      ctx.drawImage(that.data.erCodeImg,18,474,92,92) //绘制图片


      //二维码
      // ctx.drawImage(that.data.erCodeImg, (windowWidth - 50 - 50) / 2, 490, 100, 100)
      ctx.draw()
      setTimeout(() => {
        // 将生成的canvas图片，转为真实图片
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          canvasId: 'myCanvas',
          success: function (res) {
            let shareImg = res.tempFilePath;
            that.setData({
              shareImg: shareImg,
            })
            wx.hideLoading();
          },
          fail: function (res) { }
        })
      }, 500)
    }
   
    
  },
  handleSetting: function (e) {
    let that = this;
    // 对用户的设置进行判断，如果没有授权，即使用户返回到保存页面，显示的也是“去授权”按钮；同意授权之后才显示保存按钮
    if (!e.detail.authSetting['scope.writePhotosAlbum']) {
      wx.showModal({
        title: '警告',
        content: '若不打开授权，则无法将图片保存在相册中！',
        showCancel: false
      })
     
    } else {
      that.gotoSaveImg() 
      
    }
  },
  gotoSaveImg() {
    var that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.shareImg,
      success() {
        wx.showToast({
          title: '保存成功'
        })
      },
      fail(res) {        
        if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          wx.showToast({
            title: '您还没授权，请点击授权按钮',
            icon:'none'
          })
          that.setData({
            saveImgBtnHidden: true,
            openSettingBtnHidden: false
          })
          return
        }      
        wx.showToast({
          title: '保存失败',
          icon: 'none'
        })
      }
    })
  },
  handleSetting: function (e) {
    let that = this;
    // 对用户的设置进行判断，如果没有授权，即使用户返回到保存页面，显示的也是“去授权”按钮；同意授权之后才显示保存按钮
    if (!e.detail.authSetting['scope.writePhotosAlbum']) {
      wx.showModal({
        title: '警告',
        content: '若不打开授权，则无法将图片保存在相册中！',
        showCancel: false
      })
      that.setData({
        saveImgBtnHidden: true,
        openSettingBtnHidden: false
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '您已授权，赶紧将图片保存在相册中吧！',
        showCancel: false
      })
      that.setData({
        saveImgBtnHidden: false,
        openSettingBtnHidden: true
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
      return {
        title: '铅笔教育',
        desc: '',
        path: 'pages/index/index?union_user_id=' + wx.getStorageSync("userInfo").id
      }

   
  }
})